package com.zhoutian.controller;

import com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerOutputVisitor;
import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyuncs.exceptions.ClientException;
import com.zhoutian.constant.MessageConstant;
import com.zhoutian.entity.PageResult;
import com.zhoutian.entity.QueryPageBean;
import com.zhoutian.entity.Result;
import com.zhoutian.pojo.CheckGroup;
import com.zhoutian.pojo.OrderManage;
import com.zhoutian.pojo.Setmeal;
import com.zhoutian.service.OrderManageService;
import com.zhoutian.utils.SMSUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RestController
@RequestMapping("/ordermanage")
public class OrderManageController {

    @Reference
    private OrderManageService orderManageService;

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean pageBean){
        return orderManageService.findPage(pageBean);
    }

    //根据id取消预约
    @PreAuthorize("hasAuthority('ORDER_MANAGE_CANCEL')")//权限校验
    @RequestMapping("/cancel")
    public Result cancel(@RequestBody OrderManage orderManage){
        try{
            orderManageService.cancel(orderManage.getId());//发送请求
            try {
                SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE,orderManage.getPhoneNumber(),"6666");
            } catch (ClientException e) {
                e.printStackTrace();
            }
            return new Result(true, MessageConstant.CANCEL_ORDER_SUCCESS);//取消预约成功,已发送短信提醒客户预约已被取消
        }catch (Exception e){
            String message = e.getMessage();
            e.printStackTrace();
            return new Result(false, MessageConstant.CANCEL_ORDER_FAIL);//取消预约失败
        }
    }

    //根据id删除预约信息
    @PreAuthorize("hasAuthority('ORDER_MANAGE_DELETE')")//权限校验
    @RequestMapping("/delete")
    public Result delete(@RequestBody OrderManage orderManage){
        try{
            orderManageService.delete(orderManage.getId());//发送请求
            return new Result(true, MessageConstant.DELETE_ORDER_SUCCESS);//删除预约成功
        }catch (Exception e){
            String message = e.getMessage();
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ORDER_FAIL);//删除预约失败
        }
    }

    //查询全部套餐信息
    @RequestMapping("/allSetmeals")
    public Result allSetmeals(){
        try{
            List<Setmeal> list = orderManageService.findAllSetmeals();
            return new Result(true, MessageConstant.QUERY_SETMEALLIST_SUCCESS,list);//查询套餐列表数据成功
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEALLIST_FAIL);//查询套餐列表数据失败
        }
    }

    //添加预约
    @PreAuthorize("hasAuthority('ORDER_MANAGE_ADD')")//权限校验
    @RequestMapping("/add")
    public Result add(@RequestBody OrderManage orderManage, Integer[] checkitemIds){
        System.out.println("orderManage = " + orderManage);
        Integer setmealId = checkitemIds[0];
        System.out.println("setmealId = " + setmealId);
        Result result = null;
        try{
            result = orderManageService.add(orderManage, setmealId);
            return new Result(false, MessageConstant.ORDER_SUCCESS);//预约成功
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDER_FULL);//预约失败
        }

    }

}
